package com.water.camera.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.SeekBar;

import androidx.annotation.Nullable;

import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.databinding.ActivityDmEditColorMaskBinding;
import com.water.camera.mark.BaseMark;
import com.water.camera.util.ToastUtil;

public class EditSizeActivity extends BaseActivity {

    ActivityDmEditColorMaskBinding binding;
    BaseMark mark;
    float scale = 1.0f;
    String key = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDmEditColorMaskBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.generalRb.setBackground(null);
        binding.ivBack.setOnClickListener(v -> finish());
        binding.ivSave.setOnClickListener(v -> {
            SPHelper.getInstance(this).putFloat(key, scale).commit();
            ToastUtil.showShortToast(EditSizeActivity.this, "已保存");
            setResult(RESULT_OK);
            finish();
        });

        String name = getIntent().getStringExtra("name");
        try {
            mark = (BaseMark) Class.forName(name).getDeclaredConstructor(
                    Context.class,
                    AttributeSet.class
            ).newInstance(this, null);

            key = mark.getClass().getSimpleName() + "_scale";
            scale = SPHelper.getInstance(this).getFloat(key, 1.0f);
            binding.includeSize.sbSize.setProgress((int) (scale * 50));
            mark.setScaleX(scale);
            mark.setScaleY(scale);

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;
            binding.frame.addView(mark, params);

        } catch (Throwable e) {
            e.printStackTrace();
            finish();
            return;
        }
        binding.includeSize.sbSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    scale = progress * 1f / 50;
                    mark.setScaleY(scale);
                    mark.setScaleX(scale);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    protected boolean paddingTop() {
        return false;
    }
}
